﻿using Volo.Abp.Authorization.Permissions;
using Volo.Abp.DependencyInjection;

namespace YAPACS.RisServer.Authorization;

[Dependency(ReplaceServices = true)]
public class CustomStaticPermissionDefinitionStore : IStaticPermissionDefinitionStore, ITransientDependency
{
    private static Task<PermissionGroupDefinition> GetDefaultPermissionGroupDefinitionAsync()
    {
        var permissionContext = new PermissionDefinitionContext(null);
        var group = permissionContext.AddGroup("RIS");

        foreach (var permission in new List<string> { "登记", "队列", "技师", "报告", "质控", "统计" })
        {
            group.AddPermission(permission);
        }

        return Task.FromResult(group);
    }

    public async Task<IReadOnlyList<PermissionGroupDefinition>> GetGroupsAsync()
    {
        return [await GetDefaultPermissionGroupDefinitionAsync()];
    }

    public async Task<PermissionDefinition> GetOrNullAsync(string name)
    {
        var group = await GetDefaultPermissionGroupDefinitionAsync();
        return group.Permissions.FirstOrDefault(x => x.Name == name);
    }

    public async Task<IReadOnlyList<PermissionDefinition>> GetPermissionsAsync()
    {
        var group = await GetDefaultPermissionGroupDefinitionAsync();
        return group.Permissions;
    }
}
